<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
set_time_limit(0);
if (isset($_POST['o']) &&  !empty($_POST['o']) ){
include 'load.php';
$openid=addslashes($_POST['o']);
$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {}
$query=$mysqli->query("SELECT phone FROM uinfo where openid='".$openid."' limit 0,1 ");
$getPInfo=$query->fetch_array();
$query=$mysqli->query("SELECT unionid,sex FROM users where openid='".$openid."' limit 0,1 ");
$getUser=$query->fetch_array();
$WechatUnionId=$getUser['unionid'];
$url='';
$data=array(
    'phone'=>$getPInfo['phone'],
    'Name'=>addslashes($_POST['n']),
    'Gender'=>$getUser['sex'],
    'Province'=>addslashes(trim($_POST['provid'])),
    'City'=>addslashes($_POST['cityid']),
    'Address'=>addslashes(trim($_POST['a'])),
    'WechatOpenId'=>$openid,
    'unionid'=>$WechatUnionId
);

 
$curl = curl_init();  
curl_setopt($curl, CURLOPT_URL, 'https://member.elixir.com.cn/ugc/index.php/selim/user_register');                         // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);          // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);          // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);          // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);             // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1);                    // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                 // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 60);                // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0);                  // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);          // 获取的信息以文件流的形式返回
$getResult = curl_exec($curl);  

curl_close($curl); // 关闭CURL会话
$thedata=json_decode($getResult, true);
//var_dump($thedata);
if (empty($thedata['MemberNo'])){
    $mysqli->query("insert into reg_log(openid,msg,createtime) values('".$openid."','".json_encode($thedata,JSON_UNESCAPED_UNICODE)."','".date("Y-m-d H:i:s")."')  ");
    
    echo json_encode(array('s'=>'error','m'=>$thedata['msg']));
}else{
    $sql="update uinfo set isold='new',queren='yes',username='".addslashes($_POST['n'])."',sheng='".addslashes(trim($_POST['p']))."',shi='".addslashes($_POST['city'])."', pcid='".$_POST['provid'].'#'.$_POST['cityid']."',dizhi='".addslashes(trim($_POST['a']))."' where openid='".$openid."' ";
    $mysqli->query($sql);
    
    $sql="update uinfo set memberno='".$thedata['MemberNo']."'   where openid='".$openid."' ";
    $mysqli->query($sql);
    $query=$mysqli->query("SELECT openid FROM share_log where zuopenid='".$openid."' limit 0,1 ");
    if($query->num_rows>0){
        $getInfo=$query->fetch_array();
        $shareOpenid=$getInfo['openid'];
        $query=$mysqli->query("SELECT id FROM share where openid='".$shareOpenid."' and zuopenid='".$openid."' limit 0,1 ");
        if($query->num_rows>0){
        }else{
            $mysqli->query("insert into share(openid,zuopenid,createtime) values('".$shareOpenid."','".$openid."','".date("Y-m-d H:i:s")."')  ");
        }
    }
    
    
    $query=$mysqli->query("SELECT id FROM `orders` where openid='".$openid."' limit 0,1 ");
    if($query->num_rows>0){
        
    }else{
        $query=$mysqli->query("SELECT id FROM `users` where openid='".$openid."' limit 0,1 ");
        $userInfo=$query->fetch_array();
        $uid=$userInfo['id'];
        $orderId=$uid. '_' . date('YmdHis') . '_' . rand(100,999);
        $mysqli->query("insert into orders(openid,order_id,isshot,createtime) values('".$openid."','".$orderId."',0,'".date("Y-m-d H:i:s")."')  ");
    }
    
    $mysqli->query("insert into reg_log(openid,msg,createtime) values('".$openid."','".json_encode($thedata,JSON_UNESCAPED_UNICODE)."','".date("Y-m-d H:i:s")."')  ");
    
    echo json_encode(array('s'=>'ok','m'=>''));
}
$mysqli->close();

 
    
}